#include "adminmanager.h"
#include "ui_adminmanager.h"
#include "adduser.h"
#include "edituser.h"
#include "deleteuser.h"
#include "QDebug"
#include "string"
#include "QJsonDocument"
AdminManager::AdminManager(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::AdminManager)
    ,common(this)
{
    common.loadQssfromfile(":/qss/loginQSS.qss");
    Logger().init();
   // // 显示数据
   //  //创建记录
   //  QJsonObject newData;
   //  newData["username"] = "John Doe";
   //  newData["password"] = "123456";
   //  newData["email"] = "john.doe@example.com";
   //  newData["phone"] = "13170061938";
   //  newData["role"] = "user";
   //  db->create("users", newData);


   //  QJsonObject newData2;
   //  newData2["username"] = "John Doe2";
   //  newData2["password"] = "1234567";
   //  newData2["email"] = "john.doe@examples.com";
   //  newData2["phone"] = "131700619389";
   //  newData2["role"] = "admin";
   //  db->create("users", newData2);

   //  // 读取记录




    ui->setupUi(this);
    // 设置表格行数和列数
    SQLiteDatabase* db = SQLiteDatabase::getInstance();
    QJsonArray result = db->read("users", "");
    selectAllUsers(result);


}

void AdminManager::selectAllUsers(QJsonArray result)
{




    // 设置表头
    QStringList headers;
    headers << "请选择"<<"用户名" << "手机号" << "邮箱" <<"角色";




    ui->resultTable->setHorizontalHeaderLabels(headers);
    ui->resultTable->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    ui->resultTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    // 设置表格数据与表头对齐
    QString tableStyle = "QTableView::item{margin: 20px; padding: 10px;}";

    // 应用样式表
    ui->resultTable->setStyleSheet(tableStyle);
    //数据添加到表格中





    ui->resultTable->setRowCount(result.size());
    ui->resultTable->setColumnCount(5);
    // 填充表格数据
    for (int i = 0; i < result.size(); ++i) {
        QJsonObject obj = result[i].toObject();
         QTableWidgetItem* checkboxItem = new QTableWidgetItem();
        checkboxItem->setCheckState(Qt::Unchecked);
         ui->resultTable->setItem(i, 0, checkboxItem);


        ui->resultTable->setItem(i, 1, new QTableWidgetItem(obj["username"].toString()));
        ui->resultTable->setItem(i, 2, new QTableWidgetItem(obj["phone"].toString()));
        ui->resultTable->setItem(i, 3, new QTableWidgetItem(obj["email"].toString()));

        ui->resultTable->setItem(i, 4, new QTableWidgetItem(obj["role"].toString()));
    }
    // Connect checkbox state change signal to a slot
    QObject::connect(ui->resultTable, &QTableWidget::itemChanged, this, &AdminManager::onCheckboxStateChanged);
}

AdminManager::~AdminManager()
{
    delete ui;
}

void AdminManager::on_addButton_clicked()
{
    //跳转到ui文件
    addUser * adduser=new addUser();
    adduser->setFirstWindow(this);
    adduser->show();
    //传递参数

}


void AdminManager::on_EditButton_clicked()
{
    EditUser* edituser=new EditUser();

    edituser->setUiEdit(userName,phone,email);
    edituser->show();

    edituser->setFirstWindow(this);
}


void AdminManager::on_deleteButton_clicked()
{
    //跳到删除槽
    deleteUser *dele=new deleteUser();
    dele->setDeleteUserName(userName);
    dele->show();
    dele->setFirstWindow(this);

}


void AdminManager::on_queryButton_clicked()
{
     SQLiteDatabase* db = SQLiteDatabase::getInstance();
    //通过用户名，邮箱查询
    QString username = ui->usernameInput->text();
    QString email = ui->emailInput->text();
    QString phone = ui->phoneinput->text();



    QStringList conditions;

    // 检查每个输入框是否有值，并添加到条件列表中
    if (!username.isEmpty()) {
        conditions.append(QString("username = '%1'").arg(username));
    }
    if (!email.isEmpty()) {
        conditions.append(QString("email = '%1'").arg(email));
    }
    if (!phone.isEmpty()) {
        conditions.append(QString("phone = '%1'").arg(phone));
    }

    // 使用 " OR " 连接条件
    QString condition = conditions.join(" OR ");

    // 通过传递条件字符串调用数据库读取方法
    QJsonArray result = db->read("users", condition);
    //查询数据
    selectAllUsers(result);
    // qDebug().noquote() << QJsonDocument(result).toJson(QJsonDocument::Indented);
    // ui->resultTable->setRowCount(result.size());
    // ui->resultTable->setColumnCount(5);
    // for (int i = 0; i < result.size(); ++i) {
    //     QJsonObject obj = result[i].toObject();




    //     ui->resultTable->setItem(i, 0, new QTableWidgetItem(obj["username"].toString()));
    //     ui->resultTable->setItem(i, 1, new QTableWidgetItem(obj["phone"].toString()));
    //     ui->resultTable->setItem(i, 2, new QTableWidgetItem(obj["email"].toString()));

    //     ui->resultTable->setItem(i, 3, new QTableWidgetItem(obj["role"].toString()));
    // }

}

void AdminManager::onCheckboxStateChanged(QTableWidgetItem *item)
{
    // 确保是第一列的勾选框触发的
    if (item->column() == 0) {
        // 检查勾选状态
        if (item->checkState() == Qt::Checked) {
            int row = item->row();
            // 获取这一行的其他数据
            userName = ui->resultTable->item(row, 1)->text();
             phone = ui->resultTable->item(row, 2)->text();
             email = ui->resultTable->item(row, 3)->text();
             role = ui->resultTable->item(row, 4)->text();

            // 打印或处理这些数据
            qDebug() << "Selected user:" << userName<< phone << email << role;
            //进行槽函数传递
            // 可以在这里加入更多的逻辑处理
        }
    }
}



void AdminManager::ReceiveLogininformation(QString &userName, QString &role)
{
    qDebug()<<userName<<role;
    if(role=="user"){
        ui->addButton->setEnabled(false);
        ui->EditButton->setEnabled(false);
        ui->deleteButton->setEnabled(false);
        ui->addButton->setVisible(false);
        ui->EditButton->setVisible(false);
        ui->deleteButton->setVisible(false);
    }
}




